package com.atguigu.mr2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.util.Tool;

import java.io.IOException;

public class HDFSRunner extends Configured implements Tool {

    public int run(String[] args) throws Exception {

        // 得到Configuration
        Configuration conf = this.getConf();

        // 创建Job任务
        Job job = Job.getInstance(conf, this.getClass().getSimpleName());

        // 设置主类
        job.setJarByClass(HDFSRunner.class);
        Path inPath = new Path("hdfs://hadoop131:9000/input_fruit/fruit.tsv");
        FileInputFormat.addInputPath(job, inPath);

        // 设置Mapper
        job.setMapperClass(HDFSMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(Put.class);

        // 设置Reducer
        TableMapReduceUtil.initTableReducerJob("fruit_mr_02", HDFSReducer.class, job);

        // 设置Reduce数量，最少1个
        job.setNumReduceTasks(1);

        // 提交
        boolean isSuccess = job.waitForCompletion(true);
        if (!isSuccess) {
            throw new IOException("Job running with error");
        }

        return isSuccess ? 0 : 1;
    }

}
